<?php

declare(strict_types=1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class Vip extends Model
{
  //

  public static function scanUser(User $user)
  {
    $list = Vip::order('sort asc')->select();

    $last_vip = null;
    foreach ($list as $vip) {
      if ($user->getData("total_credit") >= $vip->getData('require_credit')) {
        if ($user->getData('total_checkin_times') >= $vip->getData('require_checkin_times')) {
          $last_vip = $vip;
        }
      }
    }

    if (!is_null($last_vip)) {
      if ($last_vip->id != $user->getData('vip_id')) {
        $user->vip_id = $last_vip->id;
        $user->save();
      }
    }
  }
}
